﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicketReservationSystemDataLayer
{
    public class CategoryDAO
    {
        public static IQueryable<Category> GetAllCategories(
            TicketReservationSystemDataContext context)
        {
            var categories =
                from c in context.Categories
                select c;
            return categories;
        }

        public static bool DeleteCategory(
            TicketReservationSystemDataContext context, Category category)
        {
            try
            {
                context.Categories.DeleteOnSubmit(category);
                context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool DeleteCategoryById(
            TicketReservationSystemDataContext context, int categoryId)
        {
            Category category = FindCategoryById(context, categoryId);
            return DeleteCategory(context, category);
        }

        public static Category FindCategoryById(
            TicketReservationSystemDataContext context, int categoryId)
        {
            var category =
                from c in context.Categories
                where c.Id == categoryId
                select c;
            return category.FirstOrDefault();
        }

        public static bool CreateCategory(
            TicketReservationSystemDataContext context, string categoryName)
        {
            try
            {
                Category category = new Category();
                category.Name = categoryName;
                context.Categories.InsertOnSubmit(category);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool EditCategory(
            TicketReservationSystemDataContext context, Category category, string newName)
        {
            try
            {
                category.Name = newName;
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
